package com.yict.csms.resourceplan.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.stereotype.Repository;

import com.yict.common.dao.impl.BaseDao;
import com.yict.csms.resourceplan.dao.IBoxContractorLineDao;
import com.yict.csms.resourceplan.entity.BoxContractorLine;

@Repository("boxContractorLineDao")
public class BoxContractorLineDaoImpl extends BaseDao<BoxContractorLine, Long> implements IBoxContractorLineDao {

	@Override
	public List<BoxContractorLine> searchByBcId(Long bcId) {
		if(bcId == null) {
			return null;
		}
		List<BoxContractorLine> list = new ArrayList<BoxContractorLine>();
		try {
			StringBuilder hql = new StringBuilder("from BoxContractorLine t where 1=1 ");
			hql.append(" and t.bcId = :bcId");
			Query query = this.getSession().createQuery(hql.toString());
			query.setParameter("bcId", bcId);
			list = query.list();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	@Override
	public List<BoxContractorLine> searchByBcId(Long bcId, String isDelete) {
		if(bcId == null) {
			return null;
		}
		List<BoxContractorLine> list = new ArrayList<BoxContractorLine>();
		try {
			StringBuilder hql = new StringBuilder("from BoxContractorLine t where 1=1 and (t.isDelete <> :isDelete or t.isDelete is null) and t.bcId = :bcId");
			Query query = this.getSession().createQuery(hql.toString());
			query.setParameter("isDelete", isDelete);
			query.setParameter("bcId", bcId);
			list = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public boolean backAssigned(List<Long> bcId) {
		boolean flag = false;
		try {
			String queryString = "insert into Box(boxNum, importId, shipCompanyCode, yardPosition, getOrderNum, resGetDate, boxShape, profeId, cwi," +
					" pti, pcc, pcs, pct, remarks, createDate, createPerson) select b.boxNum, b.importId, c.shipCompanyCode," +
					" b.yardPostion, b.getOrderNum, b.resGetDate, b.boxShape, c.profeId, b.cwi, b.pti, b.pcc, b.pcs, b.pct, b.remarks, b.createDate, " +
					" b.createPerson from BoxContractorLine b, BoxContractor c where b.bcId = c.bcId and b.bcId in (:bcId)";
			Query query = this.getSession().createQuery(queryString);
			query.setParameterList("bcId", bcId);
			query.executeUpdate();
			
			String queryString2 = "delete from BoxContractorLine b where b.bcId in (:bcId)";
			Query query2 = this.getSession().createQuery(queryString2);
			query2.setParameterList("bcId", bcId);
			query2.executeUpdate();
			
			String queryString3 = "delete from BoxContractor b where b.bcId in (:bcId)";
			Query query3 = this.getSession().createQuery(queryString3);
			query3.setParameterList("bcId", bcId);
			query3.executeUpdate();
			
			flag = true;
		} catch (DataAccessResourceFailureException e) {
			e.printStackTrace();
		} catch (HibernateException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		}
		
		return flag;
	}

	@Override
	public boolean removeY(Long[] bcIds) {
		boolean flag = false;
		try {
			String queryString = "delete from BoxContractorLine b where b.isDelete = 'Y' and b.bcId in (:bcIds)";
			Query query = this.getSession().createQuery(queryString);
			query.setParameterList("bcIds", bcIds);
			query.executeUpdate();
			flag = true;
		} catch (DataAccessResourceFailureException e) {
			e.printStackTrace();
		} catch (HibernateException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public BoxContractorLine findByBoxNum(String boxNum) {
		if(boxNum == null || boxNum.equals("")) {
			return null;
		}
		List<BoxContractorLine> list = new ArrayList<BoxContractorLine>();
		try {
			StringBuilder hql = new StringBuilder("from BoxContractorLine t where 1=1 and t.boxNum = :boxNum");
			Query query = this.getSession().createQuery(hql.toString());
			query.setParameter("boxNum", boxNum);
			list = query.list();
			if(list != null && list.size() > 0) {
				return list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
